package biz

import (
	"context"
	"fmt"
	pb "go-graduation/pb/router"
	"go-graduation/service/internal/pkg/output"
	"go-graduation/service/internal/service"
	"net/http"
)

func NewArticle() *Article {
	return &Article{}
}

type Article struct {
}

// GetOneArticleById 根据ID获取某篇文章信息
func GetOneArticleById(ctx context.Context, in *pb.ArticleDetailRequest) pb.ArticleDetailResponse {
	id := int(in.Id)
	if id <= 0 {
		return pb.ArticleDetailResponse{
			Code: output.ErrNoParamCode,
			Msg:  output.ErrNoParam.Error(),
		}
	}

	data, err := service.NewArticle().GetOneArticleById(id)
	if err != nil {
		return pb.ArticleDetailResponse{
			Code: output.ErrNoRecordCode,
			Msg:  output.ErrNoRecord.Error(),
		}
	}
	return pb.ArticleDetailResponse{
		Data: &pb.ArticleDetailResponse_Data{
			Detail: data,
		},
		Code: http.StatusOK,
		Msg:  "success",
	}
}

// GetArticleByPage 获取全部文章
func GetArticleByPage(ctx context.Context, in *pb.ArticleListRequest) pb.ArticleListResponse {
	var res pb.ArticleListResponse
	var page int = 1
	if in.Page > 0 {
		page = int(in.Page)
	}
	list, err := service.NewArticle().GetArticleByPage(page)
	if err != nil {
		//output.NewOutput().Failure(ctx, output.ErrNoRecordCode, output.ErrNoRecord.Error())
		return res
	}

	fmt.Printf("list = %#v", list)

	return pb.ArticleListResponse{
		Data: &pb.ArticleListResponse_Data{
			List: list,
		},
		Code: http.StatusOK,
		Msg:  "success",
	}
}
